package team02.client;

import java.util.Date;

import static com.google.gwt.query.client.GQuery.*;
import team02.client.core.test.Examinee;
import team02.client.core.test.Footer;
import team02.client.core.test.Test;
import team02.client.core.test.question.Question;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

class TestReceiver implements AsyncCallback<Test> {

	@Override
	public void onFailure(Throwable caught) {
		caught.printStackTrace(System.out);
		Window.alert(caught.getMessage());
	}

	@Override
	public void onSuccess(Test test) {
		
		try {
			$("#test_title").html(test.getTitle());
			$("#test_subtitle").html(test.getSubtitle());
			$("#start_time").html(new Date(test.getStartTime()).toString());
			$("#end_time").html(new Date(test.getEndTime()).toString());

			//System.out.println(test.getTitle());
			
			String nameList = new String("<br />");
			for (Examinee examinee : test.getExamineeList()) {
				nameList += new String("解答者：　") + examinee.getName() + " ID: " + examinee.getId() + " PW: "
						+ examinee.getPassword() + "<br />";
			}
			$(".right").html(nameList);
			String statisticList = new String();
			for (String statisticType : test.getStatisticTypeList()) {

				statisticList += statisticType + "\t";
				if (Footer.STATISTIC_TYPE_ESTIMATE.equals(statisticType)) {
					statisticList += "Type " + test.getEstimateType() + " ";
				}
			}
			
			$(".right").append("<br /><br /><br />" + statisticList);

			for (Question question : test) {
				Window.alert(question.getContent());
			}

		} catch (NullPointerException e) {
			onFailure(e);
		} 
	}
}
